package example.collections;

import java.util.*;

/**
 * Created by lenovo on 2016/11/7 0007.
 * Collection 类的 collections.unmodifiableList() 方法来设置集合为只读
 */
public class ReadOnly {
    public static void main(String[] argv)
            throws Exception {
        List<String> stuff = Arrays.asList("a", "b");
        List list = new ArrayList<>(stuff);
        list = Collections.unmodifiableList(list);
        try {
            list.set(0, "new value");
        } catch (UnsupportedOperationException e) {
            e.printStackTrace();//因为list只读，所以在此异常。
        }
        Set set = new HashSet<>(stuff);
        set = Collections.unmodifiableSet(set);
        Map map = new HashMap();
        map = Collections.unmodifiableMap(map);
        System.out.println("集合现在是只读");
    }
}
